<?php
class Default_Form_ValidateTestimonial extends Zendvn_Validate_Form{
	public function __construct($arrParam = null, $options = null){
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_Captcha($arrParam['captchaID']),true);
		if(!$validator->isValid($arrParam['captcha'])){
			$message = $validator->getMessages();
			$this->_arrError['captcha'] = 'Mã xác nhận của bạn không chính xác';
		}
		
		
		/*=======================================================
		 * Start - validate picture
		*=======================================================*/
		
		$upload = new Zend_File_Transfer_Adapter_Http();
		if(!empty($arrParam['avatar'])){
			$fileInfo = $upload->getFileInfo('avatar');
			$fileName = $fileInfo['avatar']['name'];
		}
		if(!empty($fileName)){
			$options = array('gif','png','jpg','jpeg');
			$upload->addValidator('Extension',true,$options,'avatar')
			->addValidator('Size',true,array('min'=>'5kb','max'=>'10000kb'),'avatar');
			// 					->addValidator('MimeType', false, array('image/gif','image/jpeg','image/png'));
			if(!$upload->isValid('avatar')){
				$message = $upload->getMessages();
				$this->_arrError['avatar'] = current($message);
				$arrParam['avatar'] = '';
			}
		}
		
		/*=======================================================
		 * Start - return correct data array
		 *=======================================================*/
		$this->_arrData = $arrParam;
	}

	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		}
		return $this->_arrData;
	}

	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0){
			$flag = true;
		}
		return $flag;
	}

	public function getMessageErrors(){
		return $this->_arrError;
	}
	
	public function upload(){
		$config = new Zendvn_Controller_Config(array('module'=>'admin',
				'fileConfig'=>'patient-testimonial.ini'));
		$categoryConfig = $config->getConfig('patient');
		/*=======================================================
		 * Start - upload picture
		*=======================================================*/
	
		$upload = new Zendvn_File_Upload();
		if(!empty($this->_arrData['avatar'])){
			$fileInfo = $upload->getFileInfo('avatar');
			$fileName = $fileInfo['avatar']['name'];
		}
	
		if(!empty($fileName)){
			$options = array('custom_rename'=> true,'uploadDir'=> $categoryConfig['uploadDir']);
			$fileName = $upload->upload($categoryConfig['uploadDir'],'avatar',$options);
			$arrFile['avatar'] = $fileName;
			$arrParam = array(
					'uploadDir' => $categoryConfig['uploadDir'],
					'thumb-width' => array($categoryConfig['thumb-width']),
					'thumb-height' => array($categoryConfig['thumb-height']),
					'thumbDir'	=> array($categoryConfig['thumbDir'])
			);
			$options = array('type' => array($categoryConfig['thumbResize']));
			$img = Zendvn_File_Image::resizeImg($fileName, $arrParam, $options);
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($categoryConfig['uploadDir'], $this->_arrData['current_picture']);
				$upload->remove($categoryConfig['thumbDir'], $this->_arrData['current_picture']);
			}
		}
		return $arrFile;
	}

}















